import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import { qiankunWindow } from 'vite-plugin-qiankun/dist/helper'
import { APP_NAME } from '@/globals';
// 导入路由数据处理函数
// import { getRouteData } from '@/utils/import-module';

// 获取布局路由和导出路由
// const { layoutRoutes, exportRoutes } = getRouteData()

const getBread = (str: string) => ({ label: str })

// 路由配置
const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Main',
    redirect: { name: 'UserManage' },
    component: () => import('@/pages/Layout.vue'),
    children: [
      // 带有基座的路由配置
      {
        path: '/menu-manage',
        name: 'MenuManage',
        component: () => import('@/pages/MenuManage.vue'),
        meta: {
          breadList: [getBread('CURD')]
        }
      },
      {
        path: '/user-manage',
        name: 'UserManage',
        component: () => import('@/pages/UserManage.vue'),
        meta: {
          breadList: [getBread('CURD')]
        }
      },
      {
        path: '/group-manage',
        name: 'GroupManage',
        component: () => import('@/pages/GroupManage.vue'),
        meta: {
          breadList: [getBread('CURD')]
        }
      },
      {
        path: '/role-manage',
        name: 'RoleManage',
        component: () => import('@/pages/RoleManage.vue'),
        meta: {
          breadList: [getBread('CURD')]
        }
      },
    ]
  }
];

// 创建路由实例
const router = createRouter({
  history: createWebHistory(qiankunWindow.__POWERED_BY_QIANKUN__
    ? `/micro/${APP_NAME}/`
    : `/${APP_NAME}`),
  routes,
});

// 全局前置守卫
router.beforeEach((to: any, from: any, next: any) => {
  // 防止相同路径重复跳转
  if (to.path === from.path) return false;

  // 未匹配到路由时重定向到404页面
  // if (to.matched.length === 0) {
  //   return next({ name: 'Error404' });
  // }

  // 继续路由导航
  next();
})

// 导出路由实例
export default router;
